package xuegao.study.mall.rbac.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.ums.UmsResourceCategory;
import xuegao.study.mall.rbac.service.UmsResourceCategoryService;

import java.util.List;

@RestController
@RequestMapping("/resourceCategory")
public class UmsResourceCategoryController {

    @Autowired
    private UmsResourceCategoryService umsResourceCategoryService;

    @ApiOperation("新增资源分类")
    @PostMapping("/add")
    public CommonResult<Integer> add(@RequestBody UmsResourceCategory category) {
        int count = umsResourceCategoryService.add(category);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除资源分类")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Integer> delete(@PathVariable("id") Long id) {
        int count = umsResourceCategoryService.deleteById(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("更新资源分类")
    @PostMapping("/update/{id}")
    public CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody UmsResourceCategory category) {
        int count = umsResourceCategoryService.update(id, category);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("查询所有资源分类")
    @GetMapping("/listAll")
    public CommonResult<List<UmsResourceCategory>> listMenu(){
        List<UmsResourceCategory> list = umsResourceCategoryService.listMenu();
        return CommonResult.success(list);
    }
}
